package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.Address
import me.leiusual.sharing.api.model.AddressRegion
import me.leiusual.sharing.repository.AddressRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-28
 * description:
 */
class MineAddressEditViewModel @Inject constructor(
    val addressRepository: AddressRepository
) : ViewModel() {

  // 获取验证码

  private val _addressSms = MutableLiveData<String>()
  val addressSms: LiveData<Data<Unit>> = switchMap(_addressSms) {
    addressRepository.addressSms(it).toLiveData()
  }

  fun addressSms(phone: String) {
    _addressSms.value = phone
  }

  // 省市区县

  private val _addressRegion = MutableLiveData<AddressRegion>()
  val addressRegion: LiveData<AddressRegion> = _addressRegion
  fun addressRegion(addressRegion: AddressRegion) {
    _addressRegion.value = addressRegion
  }

  // 创建或更新

  private val _addressUpdate = MutableLiveData<Address>()
  val addressUpdate: LiveData<Data<Unit>> = switchMap(_addressUpdate) {
    addressRepository.addressUpdate(it).toLiveData()
  }

  fun addressUpdate(address: Address) {
    _addressUpdate.value = address
  }
}